package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增
     */

    @PostMapping ("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类父级id不能为空");
            SubjectCategoryBo subjectCategoryBo= SubjectCategoryDTOConverter.INSTANCE.convertDTOtoCategoryBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBo);
            return  Result.ok(true);
        }catch (Exception e){
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return  Result.fail(e.getMessage());
        }
    }
    /**
     * 查询岗位大类
     */


    @PostMapping ("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBo subjectCategoryBo= SubjectCategoryDTOConverter.INSTANCE.convertDTOtoCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBo> subjectCategoryBoList=subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDTO> subjectCategoryDTOList=SubjectCategoryDTOConverter.INSTANCE.convertBOtoCategoryDTOList(subjectCategoryBoList);
            return  Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.info("SubjectCategoryController.queryPrimaryCategory.dto:{}", e.getMessage(),e);
            return  Result.fail("查询失败");
        }

    }
    /**
     * 根据分类id查二级分类
     */

    @PostMapping ("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类id不能为空");
            SubjectCategoryBo subjectCategoryBo= SubjectCategoryDTOConverter.INSTANCE.convertDTOtoCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBo> subjectCategoryBoList=subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDTO> subjectCategoryDTOList=SubjectCategoryDTOConverter.INSTANCE.convertBOtoCategoryDTOList(subjectCategoryBoList);
            return  Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.info("SubjectCategoryController.queryPrimaryCategory.dto:{}", e.getMessage(),e);
            return  Result.fail("查询失败");
        }

    }


    /**
     * 更新分类
     */
    @PostMapping ("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBo subjectCategoryBo= SubjectCategoryDTOConverter.INSTANCE.convertDTOtoCategoryBO(subjectCategoryDTO);
            Boolean result=subjectCategoryDomainService.update(subjectCategoryBo);
            return  Result.ok(result);
        } catch (Exception e) {
            log.info("SubjectCategoryController.update.dto:{}", e.getMessage(),e);
            return  Result.fail("更新分类失败");
        }

    }

    @PostMapping ("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBo subjectCategoryBo= SubjectCategoryDTOConverter.INSTANCE.convertDTOtoCategoryBO(subjectCategoryDTO);
            Boolean result=subjectCategoryDomainService.delete(subjectCategoryBo);
            return  Result.ok(result);
        } catch (Exception e) {
            log.info("SubjectCategoryController.delete.dto:{}", e.getMessage(),e);
            return  Result.fail("删除失败");
        }

    }


    /**
     * 查询分类及一次性标签
     */
    public  Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryCategoryAndLabel.dto:{}"
                        , JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBo subjectCategoryBo=SubjectCategoryDTOConverter.INSTANCE.convertDTOtoCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBo> subjectCategoryBoList=subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBo);
            List<SubjectCategoryDTO> doList=new LinkedList<>();
            subjectCategoryBoList.forEach(bo ->{
                SubjectCategoryDTO dto=SubjectCategoryDTOConverter.INSTANCE.convertBoToCategoryDTO(bo);
                List<SubjectLabelDTO> labelDTOList= SubjectLabelDTOConverter.INSTANCE.convertBOtoLabelDTOList(bo.getLabelBOList());
                dto.setLabelDTOList(labelDTOList);
               doList.add(dto);
            });
            return  Result.ok(doList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

}
